package com.jiliang.jiliangserver.service.impl;

import com.jiliang.jiliangserver.domain.*;
import com.jiliang.jiliangserver.mapper.JiliangMapper;
import com.jiliang.jiliangserver.mapper.MeterMapper;
import com.jiliang.jiliangserver.service.MeterService;
import com.jiliang.jiliangserver.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MeterServiceImpl implements MeterService {

    @Autowired
    private MeterMapper meterMapper;
    @Autowired
    private JiliangMapper jiliangMapper;


    @Override
    public Map selectMeterOverview() {
        Map map = meterMapper.selectMeterOverview();
        return map;
    }

    @Override
    public List<Map> exceptionInfoStatistics(MeterQuery query) {
        List<Map> meterinfo = meterMapper.selectExceptionInfo(query);
        return meterinfo;
    }

    @Override
    public Map exceptionSumStatistics(MeterQuery query) {
        //小表
        query.setMeterType("1");
        Map little = meterMapper.selectMeterExcCount(query);
        //大表
        query.setMeterType("2");
        Map big = meterMapper.selectMeterExcCount(query);
        HashMap result = new HashMap<>();
        result.put("little", little);
        result.put("big", big);
        return result;
    }

    //用户阶梯水量
    @Override
    public Map selectUserStair(MeterQuery query) {
        Map map = meterMapper.selectUserStair(query);
        return map;
    }

    //资料统计
    @Override
    public List<Map> meterInfoStatistics(MeterQuery query) {
        List<Map> maps = meterMapper.meterInfoStatistics(query);
        return maps;
    }

    //小区用水
    @Override
    public List<Map> selectDiWater(MeterQuery query) {
        List<Map> maps = meterMapper.selectDiWater(query);
        return maps;
    }

    //小区阶梯水量
    @Override
    public Map selectXqStair(MeterQuery query) {
        Map map = meterMapper.selectXqStair(query);
        return map;
    }

    //抄表情况
    // 条件：小区、月份
    @Override
    public List<Map> selectMeterReadInfo(MeterQuery query) {
        List<Map> maps = new ArrayList<>();
        //获取查询年月
        String mrmonth = query.getMrmonth();
        if(mrmonth == null || mrmonth.equals("")){
            maps = meterMapper.selectMeterReadInfo(query);
        }else{
            String[] yearMonth = mrmonth.split("\\.");
            Integer year = Integer.valueOf(yearMonth[0]);
            Integer mouth = Integer.valueOf(yearMonth[1]);
            Calendar now = Calendar.getInstance();
            // 当前年月
            int curYear = now.get(Calendar.YEAR);
            int curMonth = now.get(Calendar.MONTH) + 1;
            System.out.println(curYear);
            System.out.println(curMonth);

            //不是本月
            if(year!=curYear || mouth!=curMonth){
                maps = meterMapper.selectMeterReadHis(query);
            }else{
                maps = meterMapper.selectMeterReadInfo(query);
            }
        }

        return maps;
    }

    @Override
    public List<Map> selectCbHistory(MeterQuery query) {
        List<Map> maps = meterMapper.selectCbHistory(query);
        return maps;
    }

    @Override
    public List<Map> selectMeterReadHis(MeterQuery query) {
        List<Map> maps = meterMapper.selectMeterReadHis(query);
        return maps;
    }

    /**
     * 查询各小区抄表情况
     * @return
     */
    @Override
    public List<Map> selectXqCbState(MeterQuery query) {
        List<Map> maps = meterMapper.selectXqCbState(query);
        return maps;
    }

    /**
     * 查询集中器抄表情况
     * @return
     */
    @Override
    public List<Map> selectConcentratorState(MeterQuery query) {
        List<Map> maps = meterMapper.selectConcentratorState(query);
        return maps;
    }

    @Override
    public List<Map> selectCiWaterDetail(MeterQuery query) {
        List<Map> maps = new ArrayList<>();
        //获取查询年月
        String mrmonth = query.getMrmonth();
        if(StringUtils.isNotBlank(mrmonth)){
            String[] yearMonth = mrmonth.split("\\.");
            Integer year = Integer.valueOf(yearMonth[0]);
            Integer mouth = Integer.valueOf(yearMonth[1]);
            Calendar now = Calendar.getInstance();
            // 当前年月
            int curYear = now.get(Calendar.YEAR);
            int curMonth = now.get(Calendar.MONTH) + 1;
            System.out.println(curYear);
            System.out.println(curMonth);

            //不是本月
            if(year!=curYear || mouth!=curMonth){
                query.setIsHis("1");
            }
        }

        return meterMapper.selectCiWaterDetail(query);
    }

    /**
     * 查询营业所用水情况
     * @param query
     * @return
     */
    @Override
    public List<Map> selectWaterTrendByDept(MeterQuery query) {
        String rlmonth = query.getRlmonth();
        if(StringUtils.isBlank(rlmonth)){
            throw new RuntimeException("查询月份不能为空！");
        }
        return meterMapper.selectWaterTrendByDept(query);
    }

    @Override
    public List<Map> selectMeterDetail(MeterQuery query) {

        List<Map> maps = new ArrayList<>();
        //获取查询年月
        String mrmonth = query.getMrmonth();
        if(StringUtils.isNotBlank(mrmonth)){
            String[] yearMonth = mrmonth.split("\\.");
            Integer year = Integer.valueOf(yearMonth[0]);
            Integer mouth = Integer.valueOf(yearMonth[1]);
            Calendar now = Calendar.getInstance();
            // 当前年月
            int curYear = now.get(Calendar.YEAR);
            int curMonth = now.get(Calendar.MONTH) + 1;
            System.out.println(curYear);
            System.out.println(curMonth);

            //不是本月
            if(year!=curYear || mouth!=curMonth){
                query.setIsHis("1");
            }
        }

        maps = meterMapper.selectMiRecentWater(query);

        return maps;

    }

    //查询抄表统计
    @Override
    public List<Map> selectCbStatistics(MeterQuery query) {
        String mrmonth = query.getMrmonth();
        // 如果是当月数据


        return Collections.emptyList();
    }

    @Override
    public List<Map> selectMeterInfoList(MeterQuery query) {
        List<Map> maps = meterMapper.selectMeterInfoList(query);
        return maps;
    }

    @Override
    public List<Map> selectUserWater(MeterQuery query) {
        List<Map> maps = meterMapper.selectUserWater(query);
        return maps;
    }

    @Override
    public List<Map> changeMeterAnalysis(MeterQuery query) {
        List<Map> maps = meterMapper.changeMeterAnalysis(query);
        return maps;
    }

    //  用水趋势
    @Override
    public List<Map> selectUserRecentWater(MeterQuery query) {
        List<Map> maps = meterMapper.selectUserRecentWater(query);
        return maps;
    }

    @Override
    public List<Map> monthYearCompare(MeterQuery query) {
        List<Map> maps = meterMapper.monthYearCompare(query);
        return maps;    }

    // 设置报警监控
    @Override
    public void alarmSetting(MeterDto meterDto) {
        meterMapper.setMeterAlarm(meterDto);
    }

    @Override
    public Map selectMeterWaterSum(MeterQuery query) {
        //获取查询年月
        String mrmonth = query.getMrmonth();

        if(StringUtils.isNotBlank(mrmonth)){
            String[] yearMonth = mrmonth.split("\\.");
            Integer year = Integer.valueOf(yearMonth[0]);
            Integer mouth = Integer.valueOf(yearMonth[1]);
            Calendar now = Calendar.getInstance();
            // 当前年月
            int curYear = now.get(Calendar.YEAR);
            int curMonth = now.get(Calendar.MONTH) + 1;
            System.out.println(curYear);
            System.out.println(curMonth);

            //不是本月
            if(year!=curYear || mouth!=curMonth){
                query.setIsHis("1");
            }
        }

        Map map = meterMapper.selectMiRecentWaterSum(query);
        return map;
    }

    @Override
    public List<Custinfo> selectCustinfoList(MeterQuery query) {
        List<Custinfo> maps = jiliangMapper.selectCustinfoList(query);
        return maps;
    }

    @Override
    public void createCustinfo(Custinfo custinfo) {

    }

    @Override
    public void editCustinfo(Custinfo custinfo) {

    }

    /**
     * 拆装(销户)
     * @param requestXh
     * @return
     */
    @Override
    public List<RequestXh> selectRequestXhList(RequestXh requestXh) {
        List<RequestXh> requestXhs = meterMapper.selectRequestXhList(requestXh);
        return requestXhs;
    }

    /**
     *
     * @param gzhb
     * @return
     */
    @Override
    public List<RequestGzhb> selectRequestGzhbList(RequestGzhb gzhb) {
        List<RequestGzhb> requestXhs = meterMapper.selectRequestGzhbList(gzhb);
        return requestXhs;
    }

    @Override
    public List<Map> selectCbByMiid(MeterQuery query) {
        List<Map> maps = meterMapper.selectCbByMiid(query);
        return maps;
    }

    @Override
    public List<Map> selectXqCbByMonth(MeterQuery query) {
        List<Map> maps = meterMapper.selectXqCbByMonth(query);
        return maps;
    }

    @Override
    public void exceptionComfirm(String miid) {
        int i = meterMapper.exceptionComfirm(miid);
        if(i<=0){
            throw new RuntimeException("核实失败！");
        }
    }

    @Override
    public Map selectMonitorCounts() {
        Map map = meterMapper.selectMonitorCounts();
        return map;
    }

    @Override
    public int createConcentrator(Concentrator concentrator) {

        return meterMapper.createConcentrator(concentrator);
    }

    @Override
    public List<Concentrator> selectConcentratorList(Concentrator concentrator) {
        List<Concentrator> concentratorList = meterMapper.selectConcentratorList(concentrator);
        return concentratorList;
    }

    @Override
    public int updateConcentrator(Concentrator concentrator) {
        return meterMapper.updateConcentrator(concentrator);
    }

    @Override
    public Concentrator selectConcentratorById(Long id) {
        Concentrator concentrator = meterMapper.selectConcentratorById(id);
        return concentrator;
    }

    @Override
    public List<Map> selectDistrictinfo(MeterQuery query) {
        return meterMapper.selectDistrictinfo(query);
    }




}
